home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UUPC11QS.ARJ / CHECKTIM.C < prev    next >
C/C++ Source or Header  |  1991-12-07  |  11KB  |  244 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    c h e c k t i m . c                                             */
  3. /*                                                                    */
  4. /*    Time of day validation routine for UUPC/extended                */
  5. /*                                                                    */
  6. /*    Copyright (c) 1989, 1990, 1991 by Andrew H. Derbyshire          */
  7. /*                                                                    */
  8. /*    Change history:                                                 */
  9. /*       20 Apr 1991 Broken out of dcpsys.c                    ahd    */
  10. /*--------------------------------------------------------------------*/
  11.  
  12. /*--------------------------------------------------------------------*/
  13. /*                        System include files                        */
  14. /*--------------------------------------------------------------------*/
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20.  
  21. /*--------------------------------------------------------------------*/
  22. /*                    UUPC/extended include files                     */
  23. /*--------------------------------------------------------------------*/
  24.  
  25. #include "lib.h"
  26. #include "checktim.h"
  27.  
  28. /*--------------------------------------------------------------------*/
  29. /*                          Global variables                          */
  30. /*--------------------------------------------------------------------*/
  31.  
  32. currentfile();
  33.  
  34. /*--------------------------------------------------------------------*/
  35. /*   The following day values are based on the fact the               */
  36. /*   localtime() call returns the day of the week as a value zero     */
  37. /*   (0) through six (6), which is converted into the bit number      */
  38. /*   and then AND'ed against the date mask.                           */
  39. /*--------------------------------------------------------------------*/
  40.  
  41. #define SUN 0x80
  42. #define MON 0x40
  43. #define TUE 0x20
  44. #define WED 0x10
  45. #define THU 0x08
  46. #define FRI 0x04
  47. #define SAT 0x02
  48. #define NEVER 0x00
  49. #define WEEKEND (SAT | SUN)
  50. #define WEEKDAY (MON | TUE | WED | THU | FRI)
  51. #define ANY (WEEKEND | WEEKDAY)
  52.  
  53. /*--------------------------------------------------------------------*/
  54. /*   Table of values for schedules.  Based on values given for        */
  55. /*   legal schedule keywords in "Managing uucp and Usenet" by         */
  56. /*   O'Reilly & Associates.  Multiple entries for a single keyword    */
  57. /*   are processed in logical OR fashion, just as multiple entries    */
  58. /*   for the same host in L.sys are treated in a logical OR           */
  59. /*   fashion.                                                         */
  60. /*                                                                    */
  61. /*   Timing windows are adjusted five minutes away from higher        */
  62. /*   telephone rates in an attempt to avoid massive charges           */
  63. /*   because of inaccurate PC clocks and the fact that a telephone    */
  64. /*   call generally requires more than one minute if the system is    */
  65. /*   trying to do useful work.                                        */
  66. /*                                                                    */
  67. /*   Does not support multiple keywords in one token                  */
  68. /*   (TuFr0800-0805), but allows multiple tokens                      */
  69. /*   (Tu0800-805,Fr0800-0805) on one line.                            */
  70. /*                                                                    */
  71. /*   The NonPeak keyword has been corrected to the current (May       */
  72. /*   1989) NonPeak hours for Telenet's PC-Pursuit.  However, keep     */
  73. /*   in mind the PC-Pursuit customer agreement specifies that you     */
  74. /*   can't use PC-Pursuit to network multiple PC's, so thou shalt     */
  75. /*   not use PC-Pursuit from a central mail server.  *sigh*           */
  76. /*                                                                    */
  77. /*   I also have Reach-Out America from ATT, for which night rates    */
  78. /*   begin at 10:00 pm.  It is duly added to the table.               */
  79. /*--------------------------------------------------------------------*/
  80.  
  81. static struct Table {
  82.    char *keyword;
  83.    int wdays;
  84.    unsigned int start;
  85.    unsigned int end;
  86.  
  87. } table[] = {
  88.    "Any",     ANY,         0, 2400,
  89.    "Wk",      WEEKDAY,     0, 2400,
  90.    "Su",      SUN,         0, 2400,
  91.    "Mo",      MON,         0, 2400,
  92.    "Tu",      TUE,         0, 2400,
  93.    "We",      WED,         0, 2400,
  94.    "Th",      THU,         0, 2400,
  95.    "Fr",      FRI,         0, 2400,
  96.    "Sa",      SAT,         0, 2400,
  97.    "Evening", WEEKDAY,  1705,  755,
  98.    "Evening", WEEKEND,     0, 2400,
  99.    "Night",   WEEKDAY,  2305,  755,
  100.    "Night",   SAT,         0, 2400,
  101.    "Night",   SUN,      2305, 1655,
  102.    "NonPeak", WEEKDAY,  1805,  655, /* Subject to change at TELENET's whim */
  103.    "NonPeak", WEEKEND,     0, 2400,
  104.    "ROA",     WEEKDAY,  2205,  755, /* Reach Out America (sm) (AT&T)       */
  105.    "ROA",     SAT,         0, 2400, /* Reach Out America (sm) (AT&T)       */
  106.    "ROA",     SUN,      2205, 1655, /* Reach Out America (sm) (AT&T)       */
  107.    "Never",   NEVER,       0, 2400,
  108.    nil(char)
  109. }; /* table */
  110.  
  111. /*--------------------------------------------------------------------*/
  112. /*    c h e c k t i m e                                               */
  113. /*                                                                    */
  114. /*    Validate a time of day field                                    */
  115. /*--------------------------------------------------------------------*/
  116.  
  117. boolean checktime(const char *xtime, time_t delta)
  118. {
  119.  
  120.    struct Table *tptr;
  121.    struct tm *tm_now;
  122.    time_t secs_now;
  123.    size_t hhmm;
  124.    int   weekday;
  125.    char  buf[BUFSIZ];
  126.    char  *token;
  127.    char  *nexttoken;
  128.    char  found = 0;           /* Did not yet find current keyword    */
  129.    boolean  dial  = FALSE;    /* May dial host at this time          */
  130.    char  tdays[20];           /* String version of user tokens       */
  131.    char  tstart[20];
  132.    char  tend[20];
  133.    size_t istart;
  134.    size_t iend;
  135.    static char  *wday[] = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat" };
  136.  
  137.    strcpy(buf,xtime);         /* Copy time to local buffer we can alter */
  138.    time(&secs_now);
  139.    secs_now = secs_now + delta;
  140.    tm_now = localtime(&secs_now);
  141.                                         /* Create structure with time    */
  142.    weekday = SUN >> tm_now->tm_wday;   /* Get day of week as single bit */
  143.    hhmm = tm_now->tm_hour*100 + tm_now->tm_min;
  144.    nexttoken = buf;           /* First pass, look at start of buffer    */
  145.  
  146.    while (!(dial) && ((token = strtok(nexttoken,",")) != NULL) )
  147.    {
  148.  
  149. /*--------------------------------------------------------------------*/
  150. /*     Parse a day/time combination from the L.SYS file         *     */
  151. /*--------------------------------------------------------------------*/
  152.  
  153.       strcpy(tstart,"0000");  /* Set default times to all day           */
  154.       strcpy(tend,"2400");
  155.  
  156.       sscanf(token,"%[A-Za-z]%[0-9]-%[0-9]",tdays,tstart,tend);
  157.  
  158.       if (strlen(tstart) >= sizeof tstart)
  159.          panic();
  160.       if (strlen(tend) >= sizeof tend)
  161.          panic();
  162.       if (strlen(tdays) >= sizeof tdays)
  163.          panic();
  164.  
  165.       printmsg(8,"checktime: %s broken into '%s' from '%s' to '%s'",
  166.                token,tdays,tstart,tend);
  167.  
  168.       istart = atoi(tstart);  /* Convert start/end times to binary          */
  169.       iend  = atoi(tend);
  170.  
  171. /*--------------------------------------------------------------------*/
  172. /*    Handle case of midnight specified in such a way that the        */
  173. /*    time wraps through the daylight hours; we'll take the           */
  174. /*    conservative approach that the user really meant to start at    */
  175. /*    midnight.                                                       */
  176. /*--------------------------------------------------------------------*/
  177.  
  178.       if ((istart > iend) && (istart == 2400))
  179.          istart = 0000;
  180.  
  181. /*--------------------------------------------------------------------*/
  182. /*                  Search for the requested keyword                  */
  183. /*--------------------------------------------------------------------*/
  184.  
  185.       for (tptr = table, found = FALSE;
  186.             (tptr->keyword != nil(char)) && !dial; tptr++)
  187.       {
  188.  
  189. /*--------------------------------------------------------------------*/
  190. /*      We found the keyword, see if today qualifies for dialing      */
  191. /*--------------------------------------------------------------------*/
  192.  
  193.          if (equal(tptr->keyword,tdays))
  194.          {
  195.             found = TRUE;     /* Win or Lose, keyword is valid          */
  196.             if (weekday & tptr->wdays)    /* Can we dial out today?     */
  197.             {                             /* Yes --> Check the time     */
  198.  
  199. /*--------------------------------------------------------------------*/
  200. /*    This entry allows us to dial out today; now determine if the    */
  201. /*    time slot in the table allows dialing.                          */
  202. /*--------------------------------------------------------------------*/
  203.  
  204.                if (tptr->start > tptr->end)  /* span midnight?          */
  205.                   dial = (tptr->start <= hhmm) || (tptr->end >= hhmm);
  206.                else
  207.                   dial = (tptr->start <= hhmm) && (tptr->end >= hhmm);
  208.  
  209. /*--------------------------------------------------------------------*/
  210. /*    Now do a logical AND of that time with the time the user        */
  211. /*    specified in L.sys for this particular system.                  */
  212. /*--------------------------------------------------------------------*/
  213.  
  214.                if (istart > iend)            /* span midnight?          */
  215.                   dial = ((istart <= hhmm) || (iend >= hhmm)) && dial;
  216.                else if (istart == iend)
  217.                   dial = (istart == hhmm) && dial;
  218.                else
  219.                   dial = (istart <= hhmm) && (iend >= hhmm) && dial;
  220.             } /* if */
  221.          } /* if */
  222.       } /* for */
  223.  
  224.       if (!found)
  225.          printmsg(0,"checktime: keyword '%s' not found",token);
  226.  
  227.       nexttoken = NULL;       /* Continue parsing same string           */
  228.  
  229.    } /* while (!(dial) && ((token = strtok(nexttoken,",")) != NULL) ) */
  230.  
  231. /*--------------------------------------------------------------------*/
  232. /*            Report our results and return to the caller             */
  233. /*--------------------------------------------------------------------*/
  234.  
  235.    printmsg(3,"checktime: call window '%s' %s at %d:%02d (%s)",
  236.          dial ? token : xtime,
  237.          dial ? "open" :"closed",
  238.          tm_now->tm_hour,tm_now->tm_min,
  239.          wday[tm_now->tm_wday]);
  240.  
  241.    return (dial);
  242.  
  243. } /*checktime*/
  244.